<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('稿件管理列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>稿件类型：</label>
                            <select name="paperType" th:with="type=${@dict.getType('work_paper_type')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <label>是否销账：</label>
                            <select name="isCancel" th:with="type=${@dict.getType('work_cancel_status')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <label>是否请款：</label>
                            <select name="requestStatus" th:with="type=${@dict.getType('work_request_status')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <label>稿件名称：</label>
                            <input type="text" name="paperName"/>
                        </li>
                        <li>
                            <label>稿件所属：</label>
                            <input type="text" name="belong"/>
                        </li>
                        <li>
                            <label>客户名称：</label>
                            <input type="text" name="customerName"/>
                        </li>
                        <li>
                            <label>客户公司：</label>
                            <input type="text" name="companyName"/>
                        </li>
                        <li>
                        <label>媒体名称：</label>
                        <input type="text" name="mediaName"/>
                    </li>
                        <li>
                            <label>媒介：</label>
                            <input type="text" name="mediaCreate"/>
                        </li>
                        <li>
                            <label>平台：</label>
                            <select name="platform" th:with="type=${@dict.getType('work_platform')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <label>状态：</label>
                            <select name="paperStatus" th:with="type=${@dict.getType('work_paper_status')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <label>是否开票：</label>
                            <select name="isInvoice" th:with="type=${@dict.getType('sys_yes_no')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li class="select-time">
                            <label>登记时间： </label>
                            <input type="text" class="time-input" id="startTime" placeholder="开始时间" name="params[beginTime]"/>
                            <span>-</span>
                            <input type="text" class="time-input" id="endTime" placeholder="结束时间" name="params[endTime]"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="work:paper:add">
                <i class="fa fa-plus"></i> 添加
            </a>
            <!--                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="work:paper:edit">
                                <i class="fa fa-edit"></i> 修改
                            </a>-->
            <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()"
               shiro:hasPermission="work:paper:remove">
                <i class="fa fa-remove"></i> 删除
            </a>
            <a class="btn btn-info" onclick="$.table.importExcel()" shiro:hasPermission="work:paper:import">
                <i class="fa fa-upload"></i> 导入
            </a>
            <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="work:paper:export">
                <i class="fa fa-download"></i> 导出
            </a>
            <a class="btn btn-primary" onclick="setCustomer()" shiro:hasPermission="work:paper:changecustomer">
                <i class="fa "></i> 批量设置客户
            </a>
            <a class="btn btn-primary" onclick="changeP()" shiro:hasPermission="work:paper:changeprice">
                <i class="fa "></i> 批量改价
            </a>
            <a class="btn btn-primary" onclick="setmedia()" shiro:hasPermission="work:paper:setmedia">
                <i class="fa "></i> 批量领取
            </a>
            <a class="btn btn-info" onclick="fund()" shiro:hasPermission="work:paper:fund">
                <i class="fa "></i> 请款
            </a>
            <a class="btn btn-info" onclick="publish()" shiro:hasPermission="work:paper:publish">
                <i class="fa "></i> 刊登
            </a>
            <a class="btn btn-danger" onclick="reject()" shiro:hasPermission="work:paper:reject">
                <i class="fa "></i> 批量拒稿
            </a>
            <a class="btn btn-warning" onclick="process()" shiro:hasPermission="work:paper:process">
                <i class="fa "></i> 批量处理
            </a>
            <a class="btn btn-danger" onclick="undo()" shiro:hasPermission="work:paper:undo">
                <i class="fa "></i> 取消处理
            </a>

        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table" style="table-layout: fixed;word-break:break-all;"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>

<script th:inline="javascript">
    var addFlag = [[${@permission.hasPermi('work:paper:add')}]];
    var editFlag = [[${@permission.hasPermi('work:paper:edit')}]];
    var publishFlag = [[${@permission.hasPermi('work:paper:publish')}]];

    var removeFlag = [[${@permission.hasPermi('work:paper:remove')}]];
    var paperTypeDatas = [[${@dict.getType('work_paper_type')}]];
    var platformDatas = [[${@dict.getType('work_platform')}]];
    var priceTypeDatas = [[${@dict.getType('work_price_type')}]];
    var paperStatusDatas = [[${@dict.getType('work_paper_status')}]];
    var prefix = ctx + "work/paper";
    var path = window.location.pathname.split("/")[2].toLowerCase()

    var paperType = {
        netpaper: "1",
        wbpaper: "2",
        wxpaper: "3",
        planepaper: "4",
        apppaper: "5",
        flowpaper: "6"
    };
    var url = paperType[path];
    $(function () {
        var options = {};
        if (addFlag == "") {//没有发布稿件的权限，业务员，不是媒介

            options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                importTemplateUrl: prefix + "/importTemplate",
                importUrl: prefix + "/importData",
                modalName: "稿件管理",
                showFooter: true,
                footerStyle: footerStyle,
                striped: true,
                columns: [{
                    checkbox: true
                },
                    {
                        field: 'paperId',
                        title: '稿件ID',
                        width: 80,
                        footerFormatter: function (value) {
                            return "合计：";
                        }
                    },
                    {
                        field: 'paperStatus',
                        title: '状态',
                        formatter: function (value, row, index) {
                            return $.table.selectDictLabel(paperStatusDatas, value);
                        },
                        width: 100
                    },
                    {
                        field: 'paperType',
                        title: '稿件类型',
                        formatter: function (value, row, index) {
                            return $.table.selectDictLabel(paperTypeDatas, value);
                        },
                        width: 100

                    },
                    {
                        field: 'paperName',
                        title: '稿件名称',
                        colspan: 1,
                        width: 500,
                        widthUnit: 'px'

                    },
                    {
                        field: 'uploadFile',
                        title: '上传稿件',
                        formatter: function (value, row, index) {
                            if (value != '' && value != undefined) {
                                var fileName = value.substr(value.lastIndexOf("/") + 1);
                                return '<a  href="' + ctx + "common/download/resource?resource=" + value + '" target="_blank">' + fileName + '</a>';

                            } else {
                                return value
                            }
                        },
                        width: 200
                    },
                    {
                        field: 'belong',
                        title: '稿件所属',
                        width: 80
                    },

                    {
                        field: 'customerName',
                        title: '客户名称',
                        width: 200

                    },
                    {
                        field: 'companyName',
                        title: '客户所属公司',
                        width: 200

                    }, {
                        field: 'createTime',
                        title: '登记时间',
                        width: 200
                    },
                    {
                        field: 'mediaName',
                        title: '媒体名称',
                        formatter: function (value, row, index) {
                            return '<a  href="javascript:void(0)" onclick="viewMedia(\'' + row.paperType + '\',\'' + row.mediaId + '\')">' + value + '</a>';
                        },
                        width: 200
                    },
                    {
                        field: 'platform',
                        title: '平台',
                        formatter: function (value, row, index) {
                            return $.table.selectDictLabel(platformDatas, value);
                        },
                        width: 100

                    },
                    {
                        field: 'mediaCreate',
                        title: '媒介',
                        width: 200

                    },
                    {
                        field: 'publishLink',
                        title: '刊登链接',
                        width: 200

                    },
                    {
                        field: 'publishDate',
                        title: '刊登时间',
                        width: 100,
                    },
                    {
                        field: 'paperPrice',
                        title: '稿件费',
                        width: 100,
                        footerFormatter: function (value) {
                            var sumBalance = 0;
                            for (var i in value) {
                                sumBalance += parseFloat(value[i].paperPrice);
                            }
                            return sumBalance;
                        }
                    },
                    {
                        field: 'flowPrice',
                        title: '刷量费',
                        width: 100

                    },
                    {
                        field: 'cost',
                        title: '成本',
                        width: 100,
                        footerFormatter: function (value) {
                            var sumBalance = 0;
                            for (var i in value) {
                                sumBalance += parseFloat(value[i].cost);
                            }
                            return sumBalance;
                        }
                    },
                    {
                        field: 'actualCost',
                        title: '实际成本',
                        width: 100,
                        footerFormatter: function (value) {
                            var sumBalance = 0;
                            for (var i in value) {
                                sumBalance += parseFloat(value[i].actualCost);
                            }
                            return sumBalance;
                        }
                    },
                    {
                        field: 'price',
                        title: '单价',
                        width: 100
                    },
                    {
                        field: 'words',
                        title: '字数',
                        width: 100
                    },
                    {
                        field: 'actualPrice',
                        title: '实际总价',
                        width: 100,
                        footerFormatter: function (value) {
                            var sumBalance = 0;
                            for (var i in value) {
                                sumBalance += parseFloat(value[i].actualPrice);
                            }
                            return sumBalance;
                        }
                    },
                    {
                        field: 'priceType',
                        title: '价格类型',
                        formatter: function (value, row, index) {
                            return $.table.selectDictLabel(priceTypeDatas, value);
                        },
                        width: 100
                    },
                    {
                        field: 'offAccountDate',
                        title: '销账时间',
                        width: 100
                    },
                    {
                        field: 'commision',
                        title: '提成',
                        width: 100
                    },
                    {
                        field: 'commisionDate',
                        title: '提成时间',
                        width: 100
                    },
                    {
                        field: 'flowRemark',
                        title: '刷量备注',
                        width: 200
                    },
                    {
                        field: 'originalLink',
                        title: '原始链接',
                        width: 200
                    },
                    {
                        field: 'paybackPeriod',
                        title: '回款周期',
                        width: 100
                    },
                    {
                        field: 'remark',
                        title: '备注',
                        width: 200
                    }]
            };
        } else if (publishFlag == "") {

            options = {
                url: prefix + "/list/" + url,
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                importTemplateUrl: prefix + "/importTemplate",
                importUrl: prefix + "/importData",
                modalName: "稿件管理",
                showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                showFooter: true,
                footerStyle: footerStyle,
                striped: true,
                columns: [{
                    checkbox: true
                },
                    {
                        field: 'paperId',
                        title: '稿件ID',
                        width: 80,
                        footerFormatter: function (value) {
                            return "合计：";
                        }
                    },
                    {
                        field: 'publishDate',
                        title: '刊登时间',
                        width: 100,
                    },
                    {
                        field: 'paperStatus',
                        title: '状态',
                        formatter: function (value, row, index) {
                            return $.table.selectDictLabel(paperStatusDatas, value);
                        },
                        width: 100
                    },

                    {
                        field: 'paperName',
                        title: '稿件名称',
                        colspan: 1,
                        width: 500,
                        widthUnit: 'px'

                    }, {
                        field: 'platform',
                        title: '平台',
                        formatter: function (value, row, index) {
                            return $.table.selectDictLabel(platformDatas, value);
                        },
                        width: 100

                    },
                    {
                        field: 'uploadFile',
                        title: '上传稿件',
                        formatter: function (value, row, index) {
                            if (value != '' && value != undefined) {
                                var fileName = value.substr(value.lastIndexOf("/") + 1);
                                return '<a  href="' + ctx + "common/download/resource?resource=" + value + '" target="_blank">' + fileName + '</a>';

                            } else {
                                return value
                            }

                        },
                        width: 200
                    },
                    {
                        field: 'mediaName',
                        title: '媒体名称',
                        formatter: function (value, row, index) {
                            return '<a  href="javascript:void(0)" onclick="viewMedia(\'' + row.paperType + '\',\'' + row.mediaId + '\')">' + value + '</a>';
                        },
                        width: 200
                    },
                    {
                        field: 'paperType',
                        title: '稿件类型',
                        formatter: function (value, row, index) {
                            return $.table.selectDictLabel(paperTypeDatas, value);
                        },
                        width: 100

                    }, {
                        field: 'requestId',
                        title: '请款',
                        formatter: function (value, row, index) {

                            if (CheckIsNullOrEmpty(value)) {
                                var actions = [];
                                actions.push('<a  href="javascript:void(0)" onclick="viewFund(\'' +  value + '\')">请款信息</a> ');
                                return actions.join('');
                            } else {
                                return '未请款'
                            }

                        },
                        width: 100

                    }, {
                        field: 'contacts',
                        title: '联系人',
                        width: 100

                    }, {
                        field: 'mediaCreate',
                        title: '安排媒介',
                        width: 100


                    },
                    {
                        field: 'actualCost',
                        title: '价格',
                        width: 100,
                        footerFormatter: function (value) {
                            var sumBalance = 0;
                            for (var i in value) {
                                sumBalance += parseFloat(value[i].actualCost);
                            }
                            return sumBalance;
                        }
                    },

                    {
                        field: 'payTime',
                        title: '支付时间',
                        width: 200
                    },
                    {
                        field: 'belong',
                        title: '稿件所属',
                        width: 80
                    },
                    {
                        field: 'createTime',
                        title: '录入时间',
                        width: 200
                    },
                    {
                        field: 'publishLink',
                        title: '刊登链接',
                        width: 200
                    },
                    {
                        field: 'remark',
                        title: '备注',
                        width: 200
                    }]
            };
        }
        $.table.init(options);
    });

    //判断数据是否为Null或者undefined或者为空字符串
    function CheckIsNullOrEmpty(value) {
        //正则表达式用于判斷字符串是否全部由空格或换行符组成
        var reg = /^\s*$/
        //返回值为true表示不是空字符串
        return (value != null && value != undefined && !reg.test(value))
    }

    function footerStyle(column) {
        return {
            actualPrice: {
                css: {color: 'red', 'font-weight': 'normal'}
            },
            actualCost: {
                css: {color: 'red', 'font-weight': 'normal'}
            }, cost: {
                css: {color: 'red', 'font-weight': 'normal'}
            }, paperPrice: {
                css: {color: 'red', 'font-weight': 'normal'}
            },
        }[column.field]
    }

    //批量改价
    function changeP() {
        var ids = $.table.selectColumns('paperId');

        if (ids.length == 0) {
            $.modal.alertWarning("请至少选择一条记录改价");
            return;
        }
        layer.prompt({title: '输入价格，并确认', formType: 0}, function (text, index) {
            layer.close(index);


            $.modal.confirm("确认要批量改价选中的" + ids.length + "条数据吗?", function () {

                $.post(prefix + "/changeprice", {ids: ids.join(), price: text}, function (result) {

                    if (result.code == web_status.SUCCESS) {
                        $.modal.msgSuccess("操作成功！")
                    } else {
                        $.modal.msgError(result.msg);
                    }
                    $.table.refresh()
                });
            });
        });
    }

    //领取
    function setmedia() {
        var ids = $.table.selectColumns('paperId');

        if (ids.length == 0) {
            $.modal.alertWarning("请至少选择一条记录领取");
            return;
        }

        $.modal.confirm("确认要批量领取选中的" + ids.length + "条数据吗?", function () {

            $.post(prefix + "/setmedia", {ids: ids.join()}, function (result) {

                if (result.code == web_status.SUCCESS) {
                    $.modal.msgSuccess("操作成功！")
                } else {
                    $.modal.msgError(result.msg);
                }
                $.table.refresh()
            });
        });

    }

    //请款
    function fund() {

        var ids = $.table.selectColumns('paperId');
        if (ids.length == 0) {
            $.modal.alertWarning("请至少选择一条记录请款");
            return;
        }
        var requestId = $.table.selectColumns('requestId');
        if (requestId.length > 0) {
            $.modal.alertWarning("请选择没有请款的记录");
            return;
        }
        var contactId = $.table.selectColumns('contactId');

        if (contactId.length != 1) {
            $.modal.alertWarning("请选择相同联系人的记录请款");
            return;
        }
        $.modal.open("请款", ctx + "work/fund/add/"+ids);
    }

    function submitfund(requestId, msg) {
        if (requestId == 0) {
            $.modal.alertWarning(msg);
            return;
        }
        var ids = $.table.selectColumns('paperId');

        $.post(prefix + "/fund", {ids: ids.join(), requestId: requestId}, function (result) {

            if (result.code == web_status.SUCCESS) {
                $.modal.msgSuccess("操作成功！")
            } else {
                $.modal.msgError(result.msg);
            }
            $.table.refresh()
        });

    }
    function viewFund(requestId){
        var options = {
            title: '请款',
            url:ctx + "work/fund/look/"+requestId,
            btn: false
        };
        $.modal.openOptions(options);

    }
    //设置客户
    function setCustomer() {
        var ids = $.table.selectColumns('paperId');

        if (ids.length == 0) {
            $.modal.alertWarning("请至少选择一条记录!");
            return;
        }
        layer.prompt({title: '输入客户名称，并确认', formType: 0}, function (text, index) {
            layer.close(index);


            $.modal.confirm("确认要设置选中的" + ids.length + "条数据吗?", function () {

                $.post(prefix + "/setcustomer", {ids: ids.join(), customer: text}, function (result) {

                    if (result.code == web_status.SUCCESS) {
                        $.modal.msgSuccess("操作成功！")
                    } else {
                        $.modal.msgError(result.msg);
                    }
                    $.table.refresh()
                });
            });
        });
    }
    //刊登
    function publish() {
        var ids = $.table.selectColumns('paperId');

        if (ids.length == 0) {
            $.modal.alertWarning("请至少选择一条记录刊登");
            return;
        }
        layer.prompt({title: '输入刊登地址，并确认', formType: 2}, function (text, index) {
            layer.close(index);


            $.modal.confirm("确认要批量刊登选中的" + ids.length + "条数据吗?", function () {

                $.post(prefix + "/publish", {ids: ids.join(), publishLink: text}, function (result) {

                    if (result.code == web_status.SUCCESS) {
                        $.modal.msgSuccess("操作成功！")
                    } else {
                        $.modal.msgError(result.msg);
                    }
                    $.table.refresh()
                });
            });
        });
    }

    //批量拒稿
    function reject() {
        var ids = $.table.selectColumns('paperId');

        if (ids.length == 0) {
            $.modal.alertWarning("请至少选择一条记录拒稿");
            return;
        }
        $.modal.confirm("确认要批量拒稿选中的" + ids.length + "条数据吗?", function () {

            $.post(prefix + "/reject", {ids: ids.join()}, function (result) {

                if (result.code == web_status.SUCCESS) {
                    $.modal.msgSuccess("操作成功！")
                } else {
                    $.modal.msgError(result.msg);
                }
                $.table.refresh()
            });
        });

    }

    //批量处理process
    function process() {
        var ids = $.table.selectColumns('paperId');

        if (ids.length == 0) {
            $.modal.alertWarning("请至少选择一条记录处理");
            return;
        }
        $.modal.confirm("确认要批量处理选中的" + ids.length + "条数据吗?", function () {

            $.post(prefix + "/process", {ids: ids.join()}, function (result) {

                if (result.code == web_status.SUCCESS) {
                    $.modal.msgSuccess("操作成功！")
                } else {
                    $.modal.msgError(result.msg);
                }
                $.table.refresh()
            });
        });
    }

    //取消处理
    function undo() {
        var ids = $.table.selectColumns('paperId');

        if (ids.length == 0) {
            $.modal.alertWarning("请至少选择一条记录处理");
            return;
        }
        $.modal.confirm("确认要批量取消处理选中的" + ids.length + "条数据吗?", function () {

            $.post(prefix + "/undo", {ids: ids.join()}, function (result) {

                if (result.code == web_status.SUCCESS) {
                    $.modal.msgSuccess("操作成功！")
                } else {
                    $.modal.msgError(result.msg);
                }
                $.table.refresh()
            });
        });
    }

    var paperType = ["", "net",
        "wb",
        "wx",
        "plane",
        "app",
        "flow"
    ];

    function viewMedia(tp, mediaId) {

        var url = ctx + "work/" + paperType[tp] + "/edit/" + mediaId ;


        var options = {
            title: '查看媒体',
            url:url,
            btn: false
        };
        $.modal.openOptions(options);
    }
</script>
<!-- 导入区域 -->
<script id="importTpl" type="text/template">
    <form enctype="multipart/form-data" class="mt20 mb10">
        <div class="col-xs-offset-1">
            <input type="file" id="file" name="file"/>
            <div class="mt10 pt5">
                <!--<input type="checkbox" id="updateSupport" name="updateSupport" title="如果登录账户已经存在，更新这条数据。"> 是否更新已经存在的用户数据-->
                &nbsp; <a onclick="$.table.importTemplate()" class="btn btn-default btn-xs"><i
                    class="fa fa-file-excel-o"></i> 下载普通稿件模板</a>
                <a onclick="$.table.importTemplate(2)" class="btn btn-default btn-xs"><i
                        class="fa fa-file-excel-o"></i> 下载项目稿件模板</a>
            </div>
            <font color="red" class="pull-left mt10">
                提示：仅允许导入“xls”或“xlsx”格式文件！
            </font>
        </div>
    </form>
</script>
</body>
</html>